package com.ljf.daily.makemoneydetail.domain.validator;

import com.ljf.common.domain.validator.BasisValidator;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.StringUtil;
import com.ljf.daily.makemoneysoftware.domain.repository.DailyDatMakeMoneySoftwareRepository;
import com.ljf.daily.makemoneydetail.domain.repository.DailyDatMakeMoneyDetailRepository;
import com.ljf.daily.makemoneydetail.dto.command.DailyDatMakeMoneyDetailAccountAdjustCommand;
import com.ljf.daily.makemoneydetail.dto.command.DailyDatMakeMoneyDetailCommand;
import com.ljf.daily.makemoneydetail.dto.command.DailyDatMakeMoneyDetailImportDataCommand;
import com.ljf.daily.makemoneydetail.dto.command.DailyDatMakeMoneyDetailTemplateImportCommand;
import com.ljf.daily.makemoneysoftware.dto.query.DailyDatMakeMoneySoftwareQuery;
import com.ljf.daily.makemoneydetail.dto.query.DailyDatMakeMoneyDetailQuery;
import com.ljf.daily.makemoneydetail.model.DailyDatMakeMoneyDetail;
import com.ljf.daily.makemoneydetail.vo.DailyDatMakeMoneyDetailVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 赚钱明细记录  业务校验
 *
 * @author LJF
 * @since 2024-02-20
 */
@Component
public class DailyDatMakeMoneyDetailValidator extends BasisValidator<DailyDatMakeMoneyDetailQuery, DailyDatMakeMoneyDetailCommand, DailyDatMakeMoneyDetailVO, DailyDatMakeMoneyDetail, DailyDatMakeMoneyDetailRepository> {

    @Resource
    private DailyDatMakeMoneySoftwareRepository softwareRepository;

    @Override
    public void checkAdd(DailyDatMakeMoneyDetailCommand command) throws ServiceException {
        super.checkAdd(command);
        if (softwareRepository.count(DailyDatMakeMoneySoftwareQuery.builder().code(command.getSoftwareCode()).build()) == 0) {
            throw new ServiceException("新增校验异常:归属赚钱软件不存在");
        }
    }

    /**
     * 校验导入数据 业务校验
     *
     * @param command
     * @throws ServiceException
     */
    public void checkImportData(DailyDatMakeMoneyDetailImportDataCommand command) throws ServiceException {
        if (command == null || CollectionUtil.isEmpty(command.getDataList())) {
            throw new ServiceException("导入异常:请输入数据");
        }
    }

    /**
     * 账户调整 业务校验
     *
     * @param command
     * @throws ServiceException
     */
    public void checkAccountAdjust(DailyDatMakeMoneyDetailAccountAdjustCommand command) throws ServiceException {

    }

    /**
     * 模板导入  业务校验
     *
     * @param command
     * @throws ServiceException
     */
    public void checkTemplateImport(DailyDatMakeMoneyDetailTemplateImportCommand command) throws ServiceException {
        if (CollectionUtil.isEmpty(command.getDataList())) {
            throw new ServiceException("模板导校验异常:无数据导入");
        }
        if (command.getDataList().stream().anyMatch(one -> StringUtil.isEmpty(one.getSoftwareAutoCode()))) {
            throw  new ServiceException("模板导校验异常:无法确定自动记录");
        }
    }

}
